#ifndef GTL_MEMORY_TREE_ALLOCATOR_HEADER_GUARD
#define GTL_MEMORY_TREE_ALLOCATOR_HEADER_GUARD

namespace GTL
{
	namespace Memory
	{
		namespace TreeAllocator
		{
			struct Header
			{
				Header* Parent;
				Header* FirstChild;
				Header* LastChild;
				Header* NextSibling;
				unsigned int UserMask;
				const char* Name;
				size_t AllocationSize;

				Header()
					: Parent(0), FirstChild(0), LastChild(0), NextSibling(0), UserMask(0), Name(0), AllocationSize(0)
				{

				}
			};

<<<<<<< .mine
#define GTL_talloc_stringify(x) #x
#define GTL_talloc_tostring(x) GTL_talloc_stringify(x)
#define GTL_talloc(type, parent) GTL::Memory::TreeAllocator::Allocate< type > ((void*)parent, 0, "[" __FILE__ "@" GTL_talloc_tostring(__LINE__) "]: " #type)
=======
#define GTL_talloc(type, parent) GTL::Memory::TreeAllocator::Allocate< type > (parent, 0, #type)
>>>>>>> .r22

			template <typename T, typename P>
			T* Allocate(P* parent, unsigned int usermask = 0, const char* name = 0)
			{
				Header* par_head = 0;
				if (parent)
					par_head = (Header*)(((char*)parent)-sizeof(Header));

				char* ptr = new char[sizeof(Header)+sizeof(T)];

				Header* this_head = (Header*)ptr;
				new (this_head) Header;
				this_head->AllocationSize = sizeof(T);
				this_head->Parent = par_head;
				this_head->UserMask = usermask;
				this_head->Name = name;

				T* this_ptr = (T*)(ptr + sizeof(Header));

				if (parent)
				{
					if (!par_head->FirstChild)
					{
						par_head->FirstChild = this_head;
						par_head->LastChild = this_head;
					} else
					{
						par_head->LastChild->NextSibling = this_head;
						par_head->LastChild = this_head;
					}
				}

				return new (this_ptr) T;
			}

			Header* GetHeader(void* ptr)
			{
				if (!ptr) return 0;

				return (Header*)(((char*)ptr)-sizeof(Header));
			}

			/// UnlinkChild
			/// GetAllocationSize
			/// GetDirectChildrenCount
			/// GetDirectChildrenSize
			/// CalculateTotalChildrenSize
			/// FreeChildren
			/// FreeChildrenMasked
			/// Free

		}
	}
}

#endif